package com.qf.usefull.dict.utils;

import com.qf.usefull.dict.entity.Word;

import java.util.Random;
import java.util.UUID;

/**
 * 单词相关的工具类，封装一些静态方法
 */
public class WordUtils {

    /**
     * 私有的构造方法可以阻止从该类的外部创建这个类对象
     */
    private WordUtils(){}

    /**
     * 生成length个随机Word对象（使用随机字符串即可）
     * @param length
     * @return
     */
    public static Word[] createWordArr(int length){

        Word[] words = new Word[length];

        for (int i = 0; i < words.length; i++) {
            Word word = new Word();
            word.setEnglish(randomStr());
            word.setChinese(randomStr());
            words[i] = word;
        }

        return words;

    }


    // 写一个方法，生成随机字符串。
    public static String randomStr(){
        return UUID.randomUUID().toString();
    }

    /**
     * 随机字符串，范围在 [a-z]之间
     * @param length 字符串的长度
     * @return
     */
    public static String randomStr(int length){
        char[] choies = new char[26];
        char start = 'a';
        for (int i = 0; i < choies.length; i++) {
            choies[i]=start++;
        }

        char[] result = new char[length];
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(length);
            result[i] = choies[index];
        }

        String s = new String(result);
        return s;

    }


}
